<?php
/**
 * Created by PhpStorm.
 * User: wudiying
 * Date: 2020/2/17
 * Time: 上午2:19
 */


/**
 * swoole之tcp异步客户端

 * new swoole_client()
    SWOOLE_SOCK_TCP //TCP协议
    SWOOLE_SOCK_ASYNC //异步支持
 */


$client = new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC);

//注册连接成功的回调
$client->on('connect',function($cli){
    $cli->send('hello');// udp用的是->sendto()
});

//注册数据接收
$client->on('receive',function($cli,$data){
    echo $data;
});

//注册连接失败
$client->on('error',function($cli){
    echo '失败';
});

//注册关闭
$client->on('close',function($cli){
    echo '关闭';
});

//发起连接
$client->connect('xxx.xxx.xxx.xxx',9501,10);